package com.wq.wrongquestion.utils;

import java.io.File;
import java.io.FileOutputStream;


import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

@Component
public class FileUploadUtils {
    
    public void uploadFile(byte[] file, String filePath, String fileName) throws Exception{
        
        // fileName=fileName.replaceAll("\\[", "").replaceAll("\\]", "");
        try {
             File targetFile=new File(filePath);
            if(!targetFile.exists()){
                if(!targetFile.mkdirs()){
                    System.out.println("创建文件夹失败");
                }
            }
            FileOutputStream fos=new FileOutputStream(targetFile.getAbsolutePath()+"/"+fileName, false);
            fos.write(file);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
       
    }

    public String getUploadFilePath(String dir){
        File file=new File("");
        try {
            file=new File(ResourceUtils.getURL(System.getProperty("user.dir")).getPath());
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(!file.exists()) file=new File("");

        File  filePath = new File(file.getAbsolutePath(),dir);

        return filePath.getAbsolutePath();
    }
}
